package com.shg.web.action;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.shg.bean.Admin;
import com.shg.bean.PageView;
import com.shg.bean.Roles;
import com.shg.service.AdminService;
import com.shg.web.tools.WebTool;

@Controller
public class AdminAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Resource AdminService adminService;
	//管理员
	private Admin admin;
	//角色
	private Roles role;
	//角色列表
	private List<Roles> roles;
	//提示信息
	private String message;
	//查询其实位置
	private Integer currentPage=1;
	//每页显示的最大记录数
	private Integer maxResult=3;
	//管理员列表
	private PageView<Admin> pageView;
	//跳转路径
	private String path;
	/**
	 * 后台主页
	 */
	public String index(){
		return "index";
	}
	/**
	 * 登陆
	 * @return
	 */
	public String login(){
		Admin login;
		try {
			login = adminService.getLogin(admin);
		} catch (Exception e1) {
			e1.printStackTrace();
			this.message="操作发生异常";
			return "error";
		}
		if(login==null){
			this.message="用户名或者密码有误,请检查后再登录!";
			return "login";
		}else{
			try {
				WebTool.saveAdmin(login);
				ServletActionContext.getResponse().sendRedirect(path);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return "login";
	}
	/**
	 * 管理员查询
	 * @return
	 */
	public String queryAdmin(){
		pageView=new PageView<Admin>(maxResult, currentPage);
		try {
			pageView.setQueryResult(adminService.queryAdmin(pageView.getFirstResult(), pageView.getMaxrecord()));
		} catch (Exception e) {
			e.printStackTrace();
			this.message="操作发生异常";
			return "error";
		}
		return "queryAdmin";
	}
	/**
	 * 用户解锁。开锁
	 * @return
	 */
	public String locked(){
		try {
			adminService.updateLock(admin);
		} catch (Exception e) {
			e.printStackTrace();
			this.message="操作发生异常";
			return "error";
		}
		return queryAdmin();
	}
	/**
	 * 修改用户信息
	 * @return
	 */
	public String editAdmin(){
		try {
			admin.setName(URLDecoder.decode(admin.getName(), "UTF-8"));
			admin.setRemarks(URLDecoder.decode(admin.getRemarks(), "UTF-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		try {
			adminService.updateAdmin(admin);
		} catch (Exception e) {
			e.printStackTrace();
			this.message="操作发生异常";
			return "error";
		}
		return queryAdmin();
	}
	/**
	 * 改变用户的角色
	 * @return
	 */
	public String changeRole(){
		try {
			adminService.updateAdminFOrRole(admin, role);
		} catch (Exception e) {
			e.printStackTrace();
			this.message="操作发生异常";
			return "error";
		}
		return SUCCESS;
	}
	/**
	 * 用户的角色修改
	 * @return
	 */
	public String editAdminForRole(){
		try {
			admin.setName(URLDecoder.decode(admin.getName(), "UTF-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		try {
			roles = adminService.getAdminForRole(admin);
		} catch (Exception e) {
			e.printStackTrace();
			this.message="操作发生异常";
			return "error";
		}
		return "editAdminForRole";
	}
	public Admin getAdmin() {
		return admin;
	}

	public void setAdmin(Admin admin) {
		this.admin = admin;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public Integer getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(Integer currentPage) {
		this.currentPage = currentPage;
	}
	public Integer getMaxResult() {
		return maxResult;
	}

	public void setMaxResult(Integer maxResult) {
		this.maxResult = maxResult;
	}

	public PageView<Admin> getPageView() {
		return pageView;
	}

	public void setPageView(PageView<Admin> pageView) {
		this.pageView = pageView;
	}
	public Roles getRole() {
		return role;
	}
	public void setRole(Roles role) {
		this.role = role;
	}
	public List<Roles> getRoles() {
		return roles;
	}
	public void setRoles(List<Roles> roles) {
		this.roles = roles;
	}

}
